package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.constant.enums.NormalAuditActionStatusEnum;
import com.engine4cloud.tdf.common.core.util.CodeUtils;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.SpAccessSpecificCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.SpAccessSpecificResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.SpAccessSpecificUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.SpAccessSpecificEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Mapper
public interface SpAccessSpecificConvert {
    SpAccessSpecificConvert INSTANCE = Mappers.getMapper(SpAccessSpecificConvert.class);

    SpAccessSpecificEntity createTo(SpAccessSpecificCreateDTO s);

    SpAccessSpecificEntity updateTo(SpAccessSpecificUpdateDTO s);

    SpAccessSpecificResultDTO convertTo(SpAccessSpecificEntity s);

    default SpAccessSpecificEntity create(SpAccessSpecificCreateDTO s, Long tenantId, NormalAuditActionStatusEnum action) {
        SpAccessSpecificEntity entity = createTo(s);
        entity.setTenantId(tenantId);
        entity.setLcbh(CodeUtils.entrustCode());
        entity.setPreAction(action);
        additionalArea(entity, s.getAreaList());
        return entity;
    }

    default SpAccessSpecificEntity update(SpAccessSpecificUpdateDTO s) {
        SpAccessSpecificEntity entity = updateTo(s);
        additionalArea(entity, s.getAreaList());
        return entity;
    }

    default SpAccessSpecificResultDTO convert(SpAccessSpecificEntity s) {
        SpAccessSpecificResultDTO result = convertTo(s);
        renderAreaList(result);
        return result;
    }

    default void additionalArea(SpAccessSpecificEntity s, List<String> areaList) {
        if (Objects.isNull(s) || CollectionUtils.isEmpty(areaList)) {
            return;
        }
        s.setSqjrszs(areaList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        s.setSqjrszds(areaList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));

        if (areaList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH) {
            s.setSqjrszxq(areaList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX));
        } else {
            s.setSqjrszxq(null);
        }

        if (areaList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH) {
            s.setSqjrszd(areaList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX));
        } else {
            s.setSqjrszd(null);
        }
    }

    default void renderAreaList(SpAccessSpecificResultDTO s) {
        if (Objects.isNull(s)) {
            return;
        }

        List<String> residenceAreaList = new ArrayList<>();
        Actions.apply(StringUtils.isNotEmpty(s.getSqjrszs()), () -> residenceAreaList.add(s.getSqjrszs()));
        Actions.apply(StringUtils.isNotEmpty(s.getSqjrszds()), () -> residenceAreaList.add(s.getSqjrszds()));
        Actions.apply(StringUtils.isNotEmpty(s.getSqjrszxq()), () -> residenceAreaList.add(s.getSqjrszxq()));
        Actions.apply(StringUtils.isNotEmpty(s.getSqjrszd()), () -> residenceAreaList.add(s.getSqjrszd()));

        s.setAreaList(residenceAreaList);
    }
}
